package com.taocore.invite.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.naming.NamingException;

import com.taocore.invite.domain.App;
import com.taocore.invite.domain.Invitation;

public class Dao {
	
	static Logger _logger = Logger.getLogger(Dao.class.getName());
	
	public static final Dao INSTANCE = new Dao();
	
	private Dao()
	{
		
	}
	
	public void updateUser(long uid, String nick, String session, short version) throws NamingException, SQLException
	{
		Connection conn = null;
		PreparedStatement statement = null;
		try
		{
			conn = DBUtils.getConnection();
			String sql = "update user_t set last_login_c=? where id_c=?";
			statement = conn.prepareStatement(sql);
			statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
			statement.setLong(4, uid);
			int result = statement.executeUpdate();
			if (result == 0)
			{
				sql = "insert into user_t(id_c, nick_c, last_login_c) values(?,?,?)";
				statement = conn.prepareStatement(sql);
				statement.setLong(1, uid);
				statement.setString(2, nick);
				statement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
			}
			statement.executeUpdate();
		}
		finally
		{
			DBUtils.close(conn, statement, null);
		}
	}
	
	public void addInvitation(long appId, String invitor, String ip, Date time, Connection conn) throws SQLException
	{
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			String sql = "insert into invite_t(app_id_c, invitor_c, ip_c, time_c) values(?,?,?,?)";
			statement = conn.prepareStatement(sql);
			statement.setLong(1, appId);
			statement.setString(2, invitor);
			statement.setString(3, ip);
			statement.setTimestamp(4, new Timestamp(time.getTime()));
			statement.executeUpdate();			
		}
		finally
		{
			DBUtils.close(null, statement, rs);
		}
	}
	
	public List<Invitation> getInvitations(long appId) throws NamingException, SQLException
	{
		List<Invitation> invitations = null;
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			conn = DBUtils.getConnection();
			String sql = "select * from invite_t where app_id_c=?";
			statement = conn.prepareStatement(sql);
			statement.setLong(1, appId);
			rs = statement.executeQuery();
			if (rs.next())
			{
				Invitation invitation = new Invitation();
				invitation.setAppId(appId);
				invitation.setInvitor(rs.getString("invitor_c"));
				invitation.setIp(rs.getString("ip_c"));
				invitation.setTime(rs.getTimestamp("time_c"));
			}
		}
		finally
		{
			DBUtils.close(conn, statement, rs);
		}
		return invitations;
	}
	
	public Invitation getInvitation(String ip, long appId, Connection conn) throws SQLException
	{
		Invitation invitation = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			String sql = "select * from invite_t where app_id_c=? and ip_c=?";
			statement = conn.prepareStatement(sql);
			statement.setLong(1, appId);
			statement.setString(2, ip);
			rs = statement.executeQuery();
			if (rs.next())
			{
				invitation = new Invitation();
				invitation.setAppId(appId);
				invitation.setInvitor(rs.getString("invitor"));
				invitation.setIp(ip);
				invitation.setTime(rs.getTimestamp("time_c"));
			}
		}
		finally
		{
			DBUtils.close(null, statement, rs);
		}
		return invitation;
	}
	
	public List<App> getApps(long userId) throws NamingException, SQLException
	{
		List<App> apps = new ArrayList<App>();
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			conn = DBUtils.getConnection();
			String sql = "select * from app_t where user_id_c=?";
			statement = conn.prepareStatement(sql);
			statement.setLong(1, userId);
			rs = statement.executeQuery();
			while (rs.next())
			{
				App app = new App();
				app.setId(rs.getLong("id_c"));
				app.setName(rs.getString("name_c"));
				apps.add(app);
			}
		}
		finally
		{
			DBUtils.close(conn, statement, rs);
		}
		return apps;
	}
	
	public void addApp(String name, long userId, Date time, Connection conn) throws SQLException
	{
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			String sql = "insert into app_t(user_id_c, name_c, time_c) values(?,?,?)";
			statement = conn.prepareStatement(sql);
			statement.setLong(1, userId);
			statement.setString(2, name);
			statement.setTimestamp(3, new Timestamp(time.getTime()));
			statement.executeUpdate();		
		}
		finally
		{
			DBUtils.close(null, statement, rs);
		}
	}
}
